package org.duiduo.controller;

import lombok.extern.slf4j.Slf4j;
import org.duiduo.annotation.AuthLogin;
import org.duiduo.constant.BaseConstant;
import org.duiduo.service.SendMessageService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 消息推送 - Controller层
 */
// 都不需要拦截
@AuthLogin(needLogin = false)
@Slf4j
@Controller
@RequestMapping("/sms")
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private SendMessageService sendMessageService;

    @GetMapping("/sendDirectMessage")
    public ModelAndView sendDirectMessage() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("testDirectExchange", BaseConstant.DIRECT_KEY_1, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    @GetMapping("/sendTopicMessage1")
    public ModelAndView sendTopicMessage1() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("topicExchange", BaseConstant.MAN, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    @GetMapping("/sendTopicMessage2")
    public ModelAndView sendTopicMessage2() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("topicExchange", BaseConstant.WOMAN, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    @GetMapping("/sendFanoutMessage")
    public ModelAndView sendFanoutMessage() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    // 失败情况一: ①消息推送到server，但是在server里找不到交换机
    @GetMapping("/testMessageAck")
    public ModelAndView testMessageAck() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("non-existent-exchange", BaseConstant.DIRECT_KEY_1, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    // 失败情况二: ②消息推送到server，找到交换机了，但是没找到队列
    @GetMapping("/testMessageAck2")
    public ModelAndView testMessageAck2() {
        ModelAndView mView = new ModelAndView();
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值：testDirectRouting 发送到交换机testDirectExchange
        rabbitTemplate.convertAndSend("lonelyDirectExchange", BaseConstant.DIRECT_KEY_1, map);
        mView.addObject("message", "推送成功~");
        mView.setViewName("info");
        return mView;
    }

    // 失败情况三: ③消息推送到sever，交换机和队列啥都没找到 ③和①一样

    // ④消息推送成功 走上面的方法

    /**
     * 发送邮箱消息
     * 
     * @return
     */
    @GetMapping("/testMail")
    public ModelAndView testMail() {
        ModelAndView mView = new ModelAndView();
        sendMessageService.send();
        mView.addObject("message", "发送邮箱消息成功~");
        mView.setViewName("info");
        return mView;
    }

}
